						    *=================================================================================================*
							* Can EU Judicial Intervention Increase Polity Scepticism? Quasi-Experimental Evidence from Spain *
							* Turnbull-Dugarte & Devine 																	  *
							*=================================================================================================*

													
//Download ESS wave 9 from: 
//https://www.europeansocialsurvey.org/data/download.html?r=9




													*********************
													**  Data wrangling **
													*********************

//Gen dummy indicator for exppsure to Junqueras ruling in EU court//
gen treatment=0
replace treatment=1 if inwmms>=12 & inwyye==2019
replace treatment=0 if inwmms==12 & inwdds<19
replace treatment=1 if inwyye==2020

label define TREAT 0"Control" 1"Treatment"
label var treatment "Exposure to Treatment"

gen votespain=.
replace votespain=1 if prtvtees==1
replace votespain=2 if prtvtees==2
replace votespain=3 if prtvtees==3
replace votespain=4 if prtvtees==5
replace votespain=3 if prtvtees==7
replace votespain=5 if prtvtees==16


label def SPAIN 1"PP" 2"PSOE" 3"UP" 4"Cs" 5"VOX"
label value votespain SPAIN
label var votespain "Vote choice in Spain"

gen voteconst=0
replace voteconst=1 if votespain==1
replace voteconst=1 if votespain==4
replace voteconst=1 if votespain==5
label def CONST 0"Voted for other party" 1"Voted for ''Constitional'' Party"
label value voteconst CONST

recode euview (0=10 "Eurosceptic") (1=9) (2=8) (3=7) (4=6) (5=5) (6=4) (7=3) (8=2) (9=1) (10=0 "Europhile"),into(euview1)
recode trstlgl (0=10 "Complete trust") (1=9) (2=8) (3=7) (4=6) (5=5) (6=4) (7=3) (8=2) (9=1) (10=0 "Complete distrust"),into(trustlegal)

recode trstplc (0=10 "Complete trust") (1=9) (2=8) (3=7) (4=6) (5=5) (6=4) (7=3) (8=2) (9=1) (10=0 "Complete distrust"),into(trustpolice)
recode trstplt (0=10 "Complete trust") (1=9) (2=8) (3=7) (4=6) (5=5) (6=4) (7=3) (8=2) (9=1) (10=0 "Complete distrust"),into(trustpolit)
recode trstprt (0=10 "Complete trust") (1=9) (2=8) (3=7) (4=6) (5=5) (6=4) (7=3) (8=2) (9=1) (10=0 "Complete distrust"),into(trustpart)
recode trstep (0=10 "Complete trust") (1=9) (2=8) (3=7) (4=6) (5=5) (6=4) (7=3) (8=2) (9=1) (10=0 "Complete distrust"),into(trustep1)
recode trstun (0=10 "Complete trust") (1=9) (2=8) (3=7) (4=6) (5=5) (6=4) (7=3) (8=2) (9=1) (10=0 "Complete distrust"),into(trstun1)
recode stfgov (0=10 "Extremely satisfied") (1=9) (2=8) (3=7) (4=6) (5=5) (6=4) (7=3) (8=2) (9=1) (10=0 "Extremely dissatisfied"),into(disatgov)
recode swd (0=10 "Extremely satisfied") (1=9) (2=8) (3=7) (4=6) (5=5) (6=4) (7=3) (8=2) (9=1) (10=0 "Extremely dissatisfied"),into(diswd)

gen escep1=.
replace escep1=1 if euview1>5 & euview1!=.
replace escep1=0 if euview1<=5 & euview1!=.


egen edmean = median(education) if cntry=="ES"
gen education1=education
replace education1=edmean if education==.

label var education1 "Education with median imputation"


replace lrscale=. if lrscale>10
egen lrmean = median(lrscale) if cntry=="ES"
gen lrscale1=lrscale
replace lrscale1=lrmean if lrscale==.


label var lrscale1 "Left-right position with median imputation"

replace incomenew=. if incomenew>10
egen incomemean = median(incomenew) if cntry=="ES"
gen income1=incomenew
replace income1=incomemean if incomenew==.

label var education1 "Income with median imputation"


													*********************
													**     Analysis	   **
													*********************


//Figure 3///

		
reg euview1 i.treatment [pweight=dweight] if cntry=="ES", robust
estimates store it1

reg euview1 i.treatment i.gender i.agecat i.education1 c.income1 c.lrscale1 [pweight=dweight] if cntry=="ES", robust
estimates store it2


logit escep1 i.treatment [pweight=dweight] if cntry=="ES", robust
margins, dydx(*) post
estimates store it10

logit escep1 i.treatment i.gender i.agecat i.education1 c.income1 c.lrscale1 [pweight=dweight] if cntry=="ES", robust
margins, dydx(*) post
estimates store it20

coefplot (it1, mlabel("β=.48 | t=3.22 | p=.0001") mlabposition(2) msymbol(circle) ///
msize(small) mlabcolor(blue) mcolor(blue) ciopts(recast(. rcap) color(blue))) (it2, mlabel("β=.47 | t=3.18 | p=.001") mlabposition(2) msymbol(diamond) ///
msize(small) mlabcolor(blue) mcolor(blue) ciopts(recast(. rcap) color(blue))), keep(*.treatment) xline(0, lpattern(dash) lwidth(thick) lcolor(cranberry%50)) ylabel("") xtitle("") title("Effect on euroscepticism (0-10)") ///
		legend(off) levels(95 90) 
		graph save itt1.gph, replace



coefplot (it10, mlabel("β=5.972 | z=2.53 | p=.012") mlabposition(2) msymbol(circle) ///
msize(small) mlabcolor(blue) mcolor(blue) ciopts(recast(. rcap) color(blue))) (it20, mlabel("β=6.197 | z=2.69 | p=.007") mlabposition(2) msymbol(diamond) ///
msize(small) mlabcolor(blue) mcolor(blue) ciopts(recast(. rcap) color(blue))), keep(*.treatment) rescale(100) xline(0, lpattern(dash) lwidth(thick) lcolor(cranberry%50)) ylabel("") xtitle("") title("Effect on Pr(Eurosceptic)") ///
		legend(position(12) row(1) size(vsmall)) levels(95 90) 
			graph save itt2.gph, replace

			grc1leg itt1.gph itt2.gph, legendfrom(itt2.gph) row(1) note("Confidence intervals at 95% and 90%", size(small) position(5)) title("Treatment effects on Euroscepticism") 




//Figure 4///
		
		
gen dissatisfied=.
replace dissatisfied=1 if diswd>5 & diswd!=.
replace dissatisfied=0 if diswd<=5 & diswd!=.
label def DISAT 0 "Satisfied with democracy" 1"Dissatisfied with democracy"
label value dissatisfied DISAT
label var dissatisfied "Dissatisfied with democracy"

reg diswd i.treatment [pweight=dweight] if cntry=="ES", robust
estimates store ita1

reg diswd i.treatment i.gender i.agecat i.education1 c.income1 c.lrscale1 [pweight=dweight] if cntry=="ES", robust
estimates store ita2

logit dissatisfied i.treatment [pweight=dweight] if cntry=="ES", robust
margins, dydx(*) post
estimates store ita10

logit dissatisfied i.treatment i.gender i.agecat i.education1 c.income1  c.lrscale1 [pweight=dweight] if cntry=="ES", robust
margins, dydx(*) post
estimates store ita20

coefplot (ita1, mlabel("β=.32 | t=2.31 | p=.021") mlabposition(2) msymbol(circle) ///
msize(small) mlabcolor(blue) mcolor(blue) ciopts(recast(. rcap) color(blue))) (ita2, mlabel("β=.30 | t=2.17 | p=.030") mlabposition(2) msymbol(diamond) ///
msize(small) mlabcolor(blue) mcolor(blue) ciopts(recast(. rcap) color(blue))), keep(*.treatment) xline(0, lpattern(dash) lwidth(thick) lcolor(cranberry%50)) ylabel("") xtitle("") title("Effect on dissatisfaction with democracy (0-10)") ///
		legend(off) levels(95 90) 
		graph save itta1.gph, replace



coefplot (ita10, mlabel("β=5.82 | z=2.11| p=.04") mlabposition(2) msymbol(circle) ///
msize(small) mlabcolor(blue) mcolor(blue) ciopts(recast(. rcap) color(blue))) (ita20, mlabel("β=5.55 | z=2.02| p=.043") mlabposition(2) msymbol(diamond) ///
msize(small) mlabcolor(blue) mcolor(blue) ciopts(recast(. rcap) color(blue))), keep(*.treatment) rescale(100) xline(0, lpattern(dash) lwidth(thick) lcolor(cranberry%50)) ylabel("") xtitle("") title("Effect on Pr(Dissatsified with democracy)") ///
		legend(position(12) row(1) size(vsmall)) levels(95 90) 
			graph save itta2.gph, replace

			grc1leg itta1.gph itta2.gph, legendfrom(itta2.gph) row(1) note("Confidence intervals at 95% and 90%", size(small) position(5)) title("Treatment effects on Dissatisfaction with democracy")




///Figure 6///

reg euview1 i.treatment [pweight=dweight] if cntry=="ES", robust
estimates store b1

reg euview1 i.treatment [pweight=dweight] if cntry=="HR", robust
estimates store b2

reg euview1 i.treatment [pweight=dweight] if cntry=="LV", robust
estimates store b3

coefplot (b1, mlabel("β=.48 | t=3.22 | p=.0001") mlabposition(6) msymbol(circle) ///
msize(small) mlabcolor(blue) mcolor(blue) ciopts(recast(. rcap) color(blue))) (b2, mlabel("β=.17 | t=.64 | p=.525") mlabposition(6) msymbol(square) ///
msize(small) mlabcolor(black) mcolor(black) ciopts(recast(. rcap) color(black))) (b3, mlabel("β=-.13 | t=-.43| p=.665") mlabposition(6) msymbol(triangle) ///
msize(small) mlabcolor(black) mcolor(black) ciopts(recast(. rcap) color(black))), keep(*.treatment) xline(0, lpattern(dash) lwidth(thick) lcolor(cranberry%50) ) ylabel("") xtitle("ITT of ECJ ruling") title("(a) Euroscepticism") ///
		legend(off) levels(95 90)

		graph save placebo2a.gph, replace

reg diswd  i.treatment [pweight=dweight] if cntry=="ES", robust
estimates store s1

reg diswd  i.treatment [pweight=dweight] if cntry=="HR", robust
estimates store s2

reg diswd  i.treatment [pweight=dweight] if cntry=="LV", robust
estimates store s3

coefplot (s1, mlabel("β=.32 | t=2.31 | p=.021") mlabposition(6) msymbol(circle) ///
msize(small) mlabcolor(blue) mcolor(blue) ciopts(recast(. rcap) color(blue))) (s2, mlabel("β=-.042 | t=-.18 | p=.860") mlabposition(6) msymbol(square) ///
msize(small) mlabcolor(black) mcolor(black) ciopts(recast(. rcap) color(black))) (s3, mlabel("β=-.128 | t=-.43| p=.670") mlabposition(6) msymbol(triangle) ///
msize(small) mlabcolor(black) mcolor(black) ciopts(recast(. rcap) color(black))), keep(*.treatment) xline(0, lpattern(dash) lwidth(thick) lcolor(cranberry%50) ) ylabel("") xtitle("ITT of ECJ ruling") title("(b) Dissatisfaction with democracy") ///
		legend(off) levels(95 90)

		graph save placebo2b.gph, replace
reg trustlegal i.treatment [pweight=dweight] if cntry=="ES", robust
estimates store c1

reg trustlegal i.treatment [pweight=dweight] if cntry=="HR", robust
estimates store c2

reg trustlegal i.treatment [pweight=dweight] if cntry=="LV", robust
estimates store c3


coefplot (c1, mlabel("β=.31 | t=2.08 | p=.038") mlabposition(6) msymbol(circle) ///
msize(small) mlabcolor(blue) mcolor(blue) ciopts(recast(. rcap) color(blue))) (c2, mlabel("β=-.02 | t=-.10 | p=.918") mlabposition(6) msymbol(square) ///
msize(small) mlabcolor(black) mcolor(black) ciopts(recast(. rcap) color(black))) (c3, mlabel("β=.28 | t=1.00 | p=..315") mlabposition(6) msymbol(triangle) ///
msize(small) mlabcolor(black) mcolor(black) ciopts(recast(. rcap) color(black))), keep(*.treatment) xline(0, lpattern(dash) lwidth(thick) lcolor(cranberry%50) ) ylabel("") xtitle("ITT of ECJ ruling") title("(c) Distrust in legal system") ///
		legend(position(6) row(1) size(vsmall)) levels(95 90)

					graph save placebo2c.gph, replace	
					
				grc1leg placebo2a.gph placebo2b.gph placebo2c.gph, legendfrom(placebo2c.gph) row(1) title("Placebo test (ii): Countries")  note("Confidence intervals at 95% and 90%", size(vsmall) position(5))


		
//Figure 5///
reg euview1 i.treatment [pweight=dweight] if cntry=="ES", robust
estimates store t1

reg diswd i.treatment [pweight=dweight] if cntry=="ES", robust
estimates store t0

reg trustlegal i.treatment [pweight=dweight] if cntry=="ES", robust
estimates store t2

reg immigrationview i.treatment [pweight=dweight] if cntry=="ES", robust
estimates store t3

reg lrscale i.treatment [pweight=dweight]if cntry=="ES", robust
estimates store t4

reg trstun1 i.treatment [pweight=dweight] if cntry=="ES", robust
estimates store t5
		
coefplot (t1, mlabel("β=.48 | t=3.22 | p=.0001") mlabposition(2) msymbol(circle) ///
msize(small) mlabcolor(blue) mcolor(blue) ciopts(recast(. rcap) color(blue))) (t0, mlabel("β=.32 | t=2.31 | p=.021") mlabposition(2) msymbol(circle) ///
msize(small) mlabcolor(blue) mcolor(blue) ciopts(recast(. rcap) color(blue)))(t2, mlabel("β=.31 | t=2.08 | p=.038") mlabposition(2) msymbol(circle) ///
msize(small) mlabcolor(blue) mcolor(blue) ciopts(recast(. rcap) color(blue))) (t5, mlabel("β=.-08 | t=-.55| p=.584") mlabposition(2) msymbol(triangle) ///
msize(small) mlabcolor(black) mcolor(black) ciopts(recast(. rcap) color(black)))(t3, mlabel("β=-.015 | t=-.11 | p=.911") mlabposition(2) msymbol(square) ///
msize(small) mlabcolor(black) mcolor(black) ciopts(recast(. rcap) color(black))) (t4, mlabel("β=.018 | t=.15 | p=.883") mlabposition(2) msymbol(diamond) ///
msize(small) mlabcolor(black) mcolor(black) ciopts(recast(. rcap) color(black))), keep(*.treatment) xline(0, lpattern(dash) lwidth(thick) lcolor(cranberry%50) ) ylabel("") xtitle("ITT effect") title("Placebo test (i): Issues") ///
		legend(position(9) size(vsmall) col(1)) levels(95 90) note("Confidence intervals at 95% and 90%", size(vsmall) position(5)) plotlabels("Euroscepticism" "Dissatisfaction with democracy" "Disrust in legal system" "Distrust in UN" "Immigration attitudes" "Left-right position")

graph save placebo_issues.gph, replace


///Figure 7///

ritest treatment _b[treatment], reps(1000) kdensityplot: reg euview1 treatment if cntry=="ES", robust
gr save permutation.gph, replace

ritest treatment _b[treatment], reps(1000) kdensityplot: reg diswd treatment if cntry=="ES", robust
gr save permutation0.gph, replace

ritest treatment _b[treatment], reps(1000) kdensityplot: reg trustlegal treatment if cntry=="ES", robust
gr save permutation1.gph, replace

ritest treatment _b[treatment], reps(1000) kdensityplot: reg trstun1 treatment if cntry=="ES", robust
gr save permutation3.gph, replace

gr combine permutation.gph permutation0.gph permutation1.gph permutation3.gph , title("Permutation tests") col(2)
gr save permutation_combined1.gph, replace



//Reg tables for appendix//
reg euview1 i.treatment [pweight=dweight] if cntry=="ES", robust
outreg2 using regtable.tex, dec(2) replace
reg euview1 i.treatment i.gender i.agecat i.education1 c.income1 c.lrscale1 [pweight=dweight] if cntry=="ES", robust
outreg2 using regtable.tex, dec(2) append
reg diswd i.treatment [pweight=dweight] if cntry=="ES", robust
outreg2 using regtable.tex, dec(2) append
reg diswd i.treatment i.gender i.agecat i.education1 c.income1 c.lrscale1 [pweight=dweight] if cntry=="ES", robust
outreg2 using regtable.tex, dec(2) append

logit escep1 i.treatment [pweight=dweight] if cntry=="ES", robust
outreg2 using regtable_binary.tex, dec(2) replace
logit escep1 i.treatment i.gender i.agecat i.education1 c.income1 c.lrscale1 [pweight=dweight] if cntry=="ES", robust
outreg2 using regtable_binary.tex, dec(2) append
logit dissatisfied i.treatment [pweight=dweight] if cntry=="ES", robust
outreg2 using regtable_binary.tex, dec(2) append
logit dissatisfied i.treatment i.gender i.agecat i.education1 c.income1 c.lrscale1 [pweight=dweight] if cntry=="ES", robust
outreg2 using regtable_binary.tex, dec(2) append


//balance test//


gen running=0 if inwmms==12 & inwdds==19
replace running=1 if inwmms==12 & inwdds==20
replace running=2 if inwmms==12 & inwdds==21
replace running=3 if inwmms==12 & inwdds==22
replace running=4 if inwmms==12 & inwdds==23
replace running=5 if inwmms==12 & inwdds==24
replace running=6 if inwmms==12 & inwdds==25
replace running=7 if inwmms==12 & inwdds==26
replace running=8 if inwmms==12 & inwdds==27
replace running=9 if inwmms==12 & inwdds==28
replace running=10 if inwmms==12 & inwdds==30
replace running=11 if inwmms==12 & inwdds==31
replace running=12 if inwmms==1 & inwdds==1
replace running=13 if inwmms==1 & inwdds==2
replace running=14 if inwmms==1 & inwdds==3
replace running=15 if inwmms==1 & inwdds==4
replace running=16 if inwmms==1 & inwdds==5
replace running=17 if inwmms==1 & inwdds==6
replace running=18 if inwmms==1 & inwdds==7
replace running=19 if inwmms==1 & inwdds==8
replace running=20 if inwmms==1 & inwdds==9
replace running=21 if inwmms==1 & inwdds==10
replace running=22 if inwmms==1 & inwdds==11
replace running=23 if inwmms==1 & inwdds==12
replace running=24 if inwmms==1 & inwdds==13
replace running=25 if inwmms==1 & inwdds==14
replace running=26 if inwmms==1 & inwdds==15
replace running=27 if inwmms==1 & inwdds==16
replace running=28 if inwmms==1 & inwdds==17
replace running=29 if inwmms==1 & inwdds==18
replace running=30 if inwmms==1 & inwdds==19
replace running=31 if inwmms==1 & inwdds==20
replace running=32 if inwmms==1 & inwdds==21
replace running=33 if inwmms==1 & inwdds==22
replace running=34 if inwmms==1 & inwdds==23
replace running=35 if inwmms==1 & inwdds==24
replace running=36 if inwmms==1 & inwdds==25
replace running=37 if inwmms==1 & inwdds==26
replace running=38 if inwmms==1 & inwdds==27


replace running=-1 if inwmms==12 & inwdds==18
replace running=-2 if inwmms==12 & inwdds==17
replace running=-3 if inwmms==12 & inwdds==16
replace running=-4 if inwmms==12 & inwdds==15
replace running=-5 if inwmms==12 & inwdds==14
replace running=-6 if inwmms==12 & inwdds==13
replace running=-7 if inwmms==12 & inwdds==12
replace running=-8 if inwmms==12 & inwdds==11
replace running=-9 if inwmms==12 & inwdds==10
replace running=-10 if inwmms==12 & inwdds==9
replace running=-11 if inwmms==12 & inwdds==8
replace running=-12 if inwmms==12 & inwdds==7
replace running=-13 if inwmms==12 & inwdds==6
replace running=-14 if inwmms==12 & inwdds==5
replace running=-15 if inwmms==12 & inwdds==4
replace running=-16 if inwmms==12 & inwdds==3
replace running=-17 if inwmms==12 & inwdds==2
replace running=-18 if inwmms==12 & inwdds==1

replace running=-19 if inwmms==11 & inwdds==30
replace running=-20 if inwmms==11 & inwdds==29
replace running=-21 if inwmms==11 & inwdds==28
replace running=-22 if inwmms==11 & inwdds==27
replace running=-23 if inwmms==11 & inwdds==26
replace running=-24 if inwmms==11 & inwdds==25
replace running=-25 if inwmms==11 & inwdds==24
replace running=-26 if inwmms==11 & inwdds==23
replace running=-27 if inwmms==11 & inwdds==22
replace running=-28 if inwmms==11 & inwdds==21
replace running=-29 if inwmms==11 & inwdds==19
replace running=-30 if inwmms==11 & inwdds==18
replace running=-31 if inwmms==11 & inwdds==17
replace running=-32 if inwmms==11 & inwdds==16
replace running=-33 if inwmms==11 & inwdds==15
replace running=-34 if inwmms==11 & inwdds==14
replace running=-35 if inwmms==11 & inwdds==13
replace running=-36 if inwmms==11 & inwdds==12
replace running=-37 if inwmms==11 & inwdds==11

logit treatment i.gender i.agecat i.education c.incomenew c.lrscale [pweight=dweight] if cntry=="ES", robust
outreg2 using balancetest1.tex, dec(2) replace
logit treatment i.gender i.agecat i.education c.incomenew c.lrscale [pweight=dweight] if cntry=="ES" & running>-10 & running<10, robust
outreg2 using balancetest1.tex, dec(2) append
logit treatment i.gender i.agecat i.education c.incomenew c.lrscale [pweight=dweight] if cntry=="ES" & running>-15 & running<15, robust
outreg2 using balancetest1.tex, dec(2) append
logit treatment i.gender i.agecat i.education c.incomenew c.lrscale [pweight=dweight] if cntry=="ES" & running>-20 & running<20, robust
outreg2 using balancetest1.tex, dec(2) append


replace votespain=6 if votespain==. & cntry=="ES"
logit treatment i.votespain [pweight=dweight] if cntry=="ES", robust
outreg2 using balancetest2.tex, dec(2) replace



